<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\Apply;
use think\Db;

/**
 * 推广员分包管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/07/28
 * Class ApplyService
 * @package app\admin\service
 */
class ApplyService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Apply();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2023/07/28
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];
		if(isset($param['game_id']) && !empty($param['game_id'])){
		    $map[] = ['game_id','=',$param['game_id']];
        }
        if(isset($param['promote_id']) && !empty($param['promote_id'])){
            $map[] = ['promote_id','=',$param['promote_id']];
        }
        if(isset($param['package_id']) && !empty($param['package_id'])){
            $map[] = ['package_id','=',$param['package_id']];
        }
        if(isset($param['status'])){
            $map[] = ['status','=',$param['status']];
        }
        if(isset($param['enable_status'])){
            $map[] = ['enable_status','=',$param['enable_status']];
        }
        $res = parent::getListNewNoCache($map);
        foreach ($res['data'] as $k=>&$v){
            $v['package_name'] = Db::name('package')->where('id',$v['package_id'])->value('package_name');
            $v['promote_name'] = Db::name('user_promote')->where('id',$v['promote_id'])->value('realname');
            $relation_package_id = Db::name('package')->where('id',$v['package_id'])->value('relation_package_id');
            if($v['enable_status'] == 1){
                $arr['promote_id'] = $v['promote_id'];
                $arr['package_id'] = $v['package_id'];
                if($v['sdk_version'] == 4){
                    $v['web_url'] = Env('domain.index_url')."/index/webIndex?f=".urlencode(base64_encode(json_encode($arr)));
                }
                $v['pc_url'] = Env('domain.index_url')."/index/pcGame?f=".urlencode(base64_encode(json_encode($arr)));
                $v['h5_url'] = Env('domain.h5_game_url')."/login?promoteId={$v['promote_id']}&packageId={$v['package_id']}&deviceId=";
                unset($arr['package_id']);
                $arr['relation_package_id'] = $relation_package_id;
                $v['pack_url'] = Env('domain.index_url')."?f=".urlencode(base64_encode(json_encode($arr)));
                $v['down_url'] = Env('domain.index_url')."/index/downGame?promote_id={$v['promote_id']}&package_id={$v['package_id']}";
                $v['down_url_new'] = Env('domain.index_url')."/index/downGameNew?promote_id={$v['promote_id']}&package_id={$relation_package_id}";
            }else{
                $v['pack_url'] = '';
                $v['pc_url'] = '';
                $v['down_url'] = '';
                $v['down_url_new'] = '';
                $v['h5_url'] = '';
                $v['web_url'] = '';
            }
        }
        return $res;
    }

    /**
     * 添加或编辑
     * @return mixed
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = isset($argList[0]) ? $argList[0] : [];
        // 是否打印SQL
        $is_sql = isset($argList[1]) ? $argList[1] : false;
        if (!$data) {
            $data = request()->param();
        }
        $data['apply_time'] = time();
        $error = '';
        $rowId = $this->model->edit($data, $error, $is_sql);
        if ($rowId) {
            return message();
        }
        return message($error, false);
    }
    public function setEnableStatus(){
        $data = request()->param();
        $error = '';
        $packageId = $this->model->where('id',$data['id'])->value('package_id');
        $packageInfo = Db::name('package')->where('id',$packageId)->find();
        if($packageInfo['sdk_version'] == 2 && !empty($packageInfo['appstore_url'])){
            $data['enable_status'] = 1;
        }
        if($packageInfo['sdk_version'] == 3){
            $data['enable_status'] = 1;
        }
        $rowId = $this->model->edit($data, $error);
        if ($rowId) {
            return message();
        }
        return message($error, false);
    }
    /**
     * 设置状态
     * @return mixed
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function status()
    {
        $data = request()->param();
        if (!$data['id']) {
            return message('记录ID不能为空', false);
        }
        if (!isset($data['status'])) {
            return message('记录状态不能为空', false);
        }
        $error = '';
        $item = [
            'id' => $data['id'],
            'status' => $data['status'],
            'enable_status' => 2,
        ];
        $packageId = $this->model->where('id',$data['id'])->value('package_id');
        $packageInfo = Db::name('package')->where('id',$packageId)->find();
        if($packageInfo['sdk_version'] == 2 && !empty($packageInfo['appstore_url'])){
            $item['enable_status'] = 1;
        }
        if($packageInfo['sdk_version'] == 3){
            $item['enable_status'] = 1;
        }
        $rowId = $this->model->edit($item, $error);
        if (!$rowId) {
            return message($error, false);
        }
        return message();
    }
    public function setPackageLandingTemplate(){
        $param = request()->param();
        $row = $this->model->setPackageLandingTemplate($param);
        return $row;
    }

    /**
     * 删除记录
     * @return array
     * @since 2024/07/03
     * @author ZhangXuanNeng
     */
    public function delete()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        $rows = $this->model->where('id','in',$ids)->select();
        $res = $this->model->where('id','in',$ids)->delete();
        if($res){
            foreach ($rows as $k=>$v){
                $oss_path = str_replace(config('admin.oss_storage.source'),'',$v['pack_url']);
                @oss_delet_object($oss_path);
            }
            return message("删除成功");
        }
        return message("删除失败", false);
    }
}